/**
 * Copyright (c) 2024-2025 National Fundamental Software of China Co., Ltd.
 *
 * You should have find a copy of NFSChina License. If not, please contact us
 * by email <os_support@nfschina>
 *
**/

/*
SPDX-FileCopyrightText: 2016 Martin Gräßlin <mgraesslin@kde.org>

SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR
LicenseRef-KDE-Accepted-GPL
*/
#include "wallpaper_integration.h"

#include <KConfig>
#include <KConfigGroup>
#include <KConfigLoader>
#include <KPackage/PackageLoader>
#include <QDebug>
#include <QFile>

#include "logging.h"

static const char *s_plasma_config = "plasma-org.kde.plasma.desktop-appletsrc";
namespace KCMWallpaper {
WallpaperIntegration::WallpaperIntegration(QObject *parent)
    : QObject(parent), m_package(KPackage::PackageLoader::self()->loadPackage(
                           QStringLiteral("Plasma/Wallpaper"))) {
    qRegisterMetaType<KConfigPropertyMap *>();
}

WallpaperIntegration::~WallpaperIntegration() = default;

void WallpaperIntegration::init() {
    if (!m_package.isValid()) {
        qCWarning(KCM_WALLPAPER) << "package is not valid";
        return;
    }
    if (auto config = configScheme()) {
        m_configuration = new KConfigPropertyMap(config, this);
        m_configuration->insert("Image", m_image);
        m_configuration->insert("PreviewImage", m_previewImage);
    }
}

void WallpaperIntegration::setPluginName(const QString &name) {
    if (m_pluginName == name) {
        return;
    }
    m_pluginName = name;
    m_package.setPath(name);
    Q_EMIT packageChanged();
}

KConfigGroup WallpaperIntegration::loadGroup() {
    KSharedConfig::Ptr config =
        KSharedConfig::openConfig(QString::fromLatin1(s_plasma_config));
    KConfigGroup configContainments = config->group("Containments");
    for (const auto &containmentId : configContainments.groupList()) {
        auto containment = configContainments.group(containmentId);
        const auto activity = containment.readEntry("activityId", QString());
        if (!activity.isEmpty()) {
            KConfigGroup group =
                containment.group("Wallpaper")
                    .group(m_pluginName)
                    .group("General"); 
            m_image = group.readEntry("Image");
            m_previewImage = group.readEntry("PreviewImage");
            qCInfo(KCM_WALLPAPER) << "Image=" << m_image << "PreviewImage=" << m_previewImage;
            return group;
        }
    }
    qCWarning(KCM_WALLPAPER) << "can't found Image config!";
    return {};
}

KConfigLoader *WallpaperIntegration::configScheme() {
    if (!m_configLoader) {
        const QString xmlPath = m_package.filePath(QByteArrayLiteral("config"),
                                                   QStringLiteral("main.xml"));
        KConfigGroup group = loadGroup();
        if (xmlPath.isEmpty()) {
            m_configLoader = new KConfigLoader(group, nullptr, this);
        } else {
            QFile file(xmlPath);
            m_configLoader = new KConfigLoader(group, &file, this);
        }
    }
    return m_configLoader;
}

} // namespace KCMWallpaper
